package com.cykj.controller;

import com.alibaba.fastjson.JSON;
import com.cykj.bean.MessageBean;
import com.cykj.bean.Tbluser;
import com.cykj.service.TestGameModule;
import com.cykj.service.UserService;
import com.cykj.util.RedisUtils;
import com.wf.captcha.ArithmeticCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping(value = "/user" , method = {RequestMethod.POST,RequestMethod.GET})
@Api(tags = "用户后台")
public class UserController {
    @Autowired
    private TestGameModule testGameModule;
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 创建验证码
     *
     * @return {@link MessageBean }
     * @author 小苏
     * @date 2023/02/02 10:14:00
     */
    @RequestMapping("createCode")
    public MessageBean createCode() throws IOException {

        //算术类型
        ArithmeticCaptcha captcha = new ArithmeticCaptcha();

        //中文类型验证吗
//        ChineseCaptcha captcha = new ChineseCaptcha();

        // 英文与数字验证码
        // SpecCaptcha captcha = new SpecCaptcha();

        //英文与数字动态验证码
        //GifCaptcha captcha = new GifCaptcha();

        //中文动态验证码
//    ChineseGifCaptcha captcha = new ChineseGifCaptcha();
        //几位数运算   默认是两位
        captcha.setLen(2);

        //获取运算结果
        String result = captcha.text();
        System.out.println("===============获取运算结果为=========:{}"+result);

        String key = UUID.randomUUID().toString();
//        redisTemplate.opsForValue().set(key, result, 2, TimeUnit.MINUTES);
        Map map = new HashMap();
        redisUtils.set("yanzhen",result);
        map.put("key", result);
        map.put("img", captcha.toBase64());
        return MessageBean.success("图片验证码", map);
    }

    /**
     * 用户登录
     *
     * @param useracc 账号
     * @param userpwd 密码
     * @return {@link MessageBean }
     * @author 小苏
     * @date 2023/02/02 10:39:16
     */
    @RequestMapping("/userLogin")
    @ApiOperation(value = "登陆", notes = "登陆" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "useracc" ,value = "账号",dataType = "String"),
            @ApiImplicitParam(name = "userpwd" ,value = "密码",dataType = "String"),
    })
    public MessageBean userLogin(String useracc,String userpwd){
        Boolean flag = userService.login(useracc, userpwd);
        Tbluser user = JSON.parseObject(redisUtils.get("user"), Tbluser.class);
        if(flag){
           return MessageBean.success("登陆成功",user.getUserid());
        }
        return MessageBean.fail("登陆失败");
    }

    @RequestMapping("/userReg")
    public MessageBean userReg(Tbluser tbluser){
        String regMsg = userService.userReg(tbluser);
        return MessageBean.success("",regMsg);
    }

    /**
     * 测试服务间调用
     *
     * @param pid pid
     * @return {@link MessageBean }
     * @author 小苏
     * @date 2023/02/08 09:53:40
     */
    @RequestMapping("/test")
    @ApiOperation(value = "测试服务间调用", notes = "测试服务间调用" )
    @ApiImplicitParam(name = "pid" ,value = "游戏类型id",dataType = "integer")
    public MessageBean test(Integer pid){
        return testGameModule.findgametype(pid);
    }
}
